<template>
  <el-row>
    <el-col>
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()" size="small">
        <el-form-item label="服务名称">
          <el-input v-model="dataForm.resourceName" style="width: 215px" placeholder="服务名称"></el-input>
        </el-form-item>
        <el-form-item label="申请时间">
          <el-date-picker
                  v-model="dataForm.useTimeRange"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="getDataList()">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table
              :data="dataList"
              border
              v-loading="dataListLoading"
              @selection-change="selectionChangeHandle"
              style="width: 100%;">
        <el-table-column
                type="selection"
                header-align="center"
                align="center"
                width="50">
        </el-table-column>
        <el-table-column
                prop="resourceId"
                header-align="center"
                align="center"
                label="id">
        </el-table-column>
        <el-table-column
                prop="resourceName"
                header-align="center"
                align="center"
                label="服务名称">
        </el-table-column>
        <el-table-column
                prop="durationName"
                header-align="center"
                align="center"
                label="申请时长">
        </el-table-column>
        <el-table-column
                prop="createdTime"
                header-align="center"
                align="center"
                width="160"
                label="申请时间">
        </el-table-column>
        <el-table-column
            prop="applyTime"
            header-align="center"
            align="center"
            width="160"
            label="审批时间">
        </el-table-column>
        <el-table-column
            prop="applyStatusName"
            header-align="center"
            align="center"
            label="审批状态">
        </el-table-column>
        <el-table-column
            prop="applyUserInfo.userName"
            header-align="center"
            align="center"
            label="审批人">
        </el-table-column>
        <el-table-column
                prop="isStartup"
                header-align="center"
                align="center"
                label="服务状态"
                :formatter="showServerStatus">
        </el-table-column>
        <el-table-column
                prop="coverageName"
                header-align="center"
                align="center"
                label="覆盖范围">
        </el-table-column>
        <el-table-column
                fixed="right"
                header-align="center"
                align="center"
                width="150"
                label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
              @size-change="sizeChangeHandle"
              @current-change="currentChangeHandle"
              :current-page="pageIndex"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="pageSize"
              :total="totalPage"
              layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </el-col>
  </el-row>
</template>

<script>
  import AddOrUpdate from './ResourceDetailed'
  import $axios from '@/network'
  import {queryApplicationApproveIPage, queryApplicationResourceIPage} from "@/api/map-apply";

  export default {
    name: "resourceList",
    data() {
      return {
        dataForm: {
          serviceName: '',
          useTimeRange: []
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    created () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList (){
        if(!this.dataForm.useTimeRange){
          this.dataForm.useTimeRange=[]
        }
        // this.dataListLoading = true
        let op={
          'serviceName': this.dataForm.serviceName,
          'startTime': this.dataForm.useTimeRange[0],
          'endTime': this.dataForm.useTimeRange[1]
        }
        this._debounce(() => queryApplicationResourceIPage(this.pageIndex, this.pageSize, op).then(res => {
          if (res.status) {
            const data = res.data;
            this.totalPage = data.total;
            this.dataList = data.records;
          }
        }), 500)
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        this.$confirm(`确定对[id=${id}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          $axios({
            url:'data/service/deleteServiceApply/'+id,
            method: 'post',
          }).then((data) => {
            if (data && data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      showStatus(row, column, cellValue, index){
        if(cellValue==1){
          return '待审批'
        }
        if(cellValue==2){
          return '已审批'
        }
        if(cellValue==3){
          return '已拒绝'
        }
      },
      showServerStatus(row, column, cellValue, index){
        if(cellValue==1){
          return '在线'
        }
        if(cellValue==0){
          return '停止'
        }
      }
    }
  }
</script>

<style scoped>

</style>